﻿$(document).ready(function () {
    $('#t_accessionno').keyup(function (e) {
        var unicode = e.charCode ? e.charCode : e.keyCode;
        if (unicode == 13 || unicode == 9) {
            get_info();
        }
    })
    $('#t_addno').keyup(function (e) {
        var unicode = e.charCode ? e.charCode : e.keyCode;
        if (unicode == 13 || unicode == 9) {
            start();
        }
    })

})
document.body.onload = function () { document.body.setAttribute("onkeypress", "short_cut(event)"); start(); };
function start() {
    $("#addedbooks").html("");
    $("#staffInfo").html("");
    $('#t_addno').focus();
    $('#total_cal_fine').html("0");
    get_info();

}
function getType() {
    var type = "";
    if (document.getElementById('IssueToStudent')) {
        if (document.getElementById('IssueToStudent').checked) {
            type = "i_s"
        }
    } 
     if (document.getElementById('IssueToStaff')) {
        if (document.getElementById('IssueToStaff').checked) {
            type = "i_st"
        }
    } 
     if (document.getElementById('ReturnFromStudent')) {
        if (document.getElementById('ReturnFromStudent').checked) {
            type = "r_s"
        }
    } 
     if (document.getElementById('ReturnFromStaff')) {
        if (document.getElementById('ReturnFromStaff').checked) {
            type = "r_st"
        }
    }
    return type;
}
function short_cut(e) {
    if (window.event) {
        key = e.keyCode;
        isAlt = e.altKey;
        isCtrl = e.ctrlKey;

    } else if (e.which) {
        key = e.which;
        isAlt = e.altKey;
        isCtrl = e.ctrlKey;
    }
   // alert(key);
    if (isCtrl && parseInt(key) == 32) {
        var type = getType();
        if (type == "i_s") {
            document.getElementById('IssueToStaff').checked = true;
        } else if (type == "i_st") {
        if (document.getElementById('ReturnFromStudent')) {
            document.getElementById('ReturnFromStudent').checked = true;
        } else if (document.getElementById('IssueToStudent')) {
            document.getElementById('IssueToStudent').checked = true;
        }
        } else if (type == "r_s") {
            document.getElementById('ReturnFromStaff').checked = true;
        } else if (type == "r_st") {
        if (document.getElementById('IssueToStudent')) {
            document.getElementById('IssueToStudent').checked = true;
        }
        else if (document.getElementById('ReturnFromStudent')) {
            document.getElementById('ReturnFromStudent').checked = true;
        }
        }
        change_option();
    }
    else if ((isAlt && parseInt(key) == 105) || (isAlt && parseInt(key) == 73)) {
     save_r_i();
    }
}
function change_option() {
    type = getType();
    document.getElementById('reissue').setAttribute("class", "none");
    if (type=='i_s' || type=="r_s") {
        document.getElementById('sinfo').innerHTML = 'Student Add No./Roll No. :';
        document.getElementById('OK').value = 'Issue';
        if (type=="r_s") {
            document.getElementById('OK').value = 'Return';
            document.getElementById('reissue').setAttribute("class","block") ;
        }
    }
    else {
        document.getElementById('sinfo').innerHTML = 'Staff Id :';
        document.getElementById('OK').value = 'Issue';
        if (type == "r_st") {
            document.getElementById('OK').value = 'Return';
            document.getElementById('reissue').setAttribute("class", "block");
        }
    }
    start();
}
function Create_Option(text, val) {
    var eleOption = document.createElement('Option')
    eleOption.text = text;
    eleOption.value = val;
    return eleOption;
}
function change_Option(val) {
    document.getElementById('sDrop').innerHTML = "";
    var ele=document.getElementById('sDrop');
    if (val == "Books") {
        ele.options.add(Create_Option("Search Book Title", "1"));
        ele.options.add(Create_Option("Search Author Name", "2"));
        ele.options.add(Create_Option("Search Book Edition", "3"));
        ele.options.add(Create_Option("Search Book Publication Name", "4"));
    }
    else if (val == "Student") {
        ele.options.add(Create_Option("Student Name","1"));
        ele.options.add(Create_Option("Add No.","2"));
        ele.options.add(Create_Option("Roll No.","3"));
    }
    else {
        ele.options.add(Create_Option("Staff Name","1"));
    }
}
function go_search() {
    var search_ele = document.getElementById('text_search');
    var search_val = "";
    var search_type="";
    if (search_ele) {
        if (search_ele.value != "") {
            search_val = search_ele.value;
        }
        else {
            alert("Please enter value for search.");
            search_ele.focus();
        }
    }
    else {
        alert("Please enter value for search.");
    }
    var R_Search_ele = document.getElementsByName('search');
    var R_Search_type = "Books";
    if (R_Search_ele) {
        for (var i = 0; i < R_Search_ele.length; i++) {
            if (R_Search_ele[i].checked) {
                R_Search_type = R_Search_ele[i].id;
            }
      }
  }
  var searc_for = document.getElementById('sDrop').value;
  senddata_post('Retrun_and_Issue', 'T=search&search_val='+search_val+'&search_type='+R_Search_type+'&search_for='+searc_for,R_Search_type);
}
function reset_val() {
    document.getElementById('staffInfo').innerHTML = '';
    document.getElementById('addedbooks').innerHTML = '';
    document.getElementById("searchResult").innerHTML = '';
    document.getElementById('t_addno').focus();
    document.getElementById('total_cal_fine').innerHTML = 0;
}
function stateChanged() {
    if (xmlhttp.readyState == 4) {
        if (receiveid == "Books") {
            //alert(xmlhttp.responseText);
            var fill = eval('(' + xmlhttp.responseText + ')');
            fillBooks(fill, 'searchResult','b');
        }
        else if (receiveid == "Student") {
            //alert(xmlhttp.responseText);
            var fill = eval('(' + xmlhttp.responseText + ')');
            fillStudents(fill, 'searchResult');
        }
        else if (receiveid == "Staff") {
            //alert(xmlhttp.responseText);
            var fill = eval('(' + xmlhttp.responseText + ')');
            fillStaff(fill, 'searchResult');
        }
        else if (receiveid == "addInfo" || receiveid == "bookInfo") {
            var fill = eval('(' + xmlhttp.responseText + ')');
            fillInfo(fill, receiveid);
        }
        else if (xmlhttp.responseText.indexOf("~", 0) > 0) {
            var arr = xmlhttp.responseText.split("~");
            if (receiveid == "lost_error") {
                document.getElementById(receiveid).innerHTML = arr[1];
                if (parseInt(arr[0]) == 1) {
                    var id = document.getElementById('f_lostid').value;
                    var ele_m = document.getElementById(id);
                    ele_m.parentNode.removeChild(ele_m);
                    cancel_lost();
                }
            }
            else {
                document.getElementById(receiveid).innerHTML = arr[1];
                document.getElementById("id").value = arr[0];
                if (arr[0] == "1") {
                    reset_val();
                }
            }
            // document.getElementById(receiveid).innerHTML = xmlhttp.responseText;
        }
    } else {
        if (receiveid == "Books" || receiveid == "Student" || receiveid == "Staff") {
            document.getElementById("searchResult").innerHTML = "<div align='center'><img src='images/loading.gif' /></div>";
        }
        else if (receiveid == "addInfo" || receiveid == "bookInfo") {
            document.getElementById("serror").innerHTML = "<div align='center'><img src='images/loading.gif' /></div>";
            //document.getElementById("addedbooks").innerHTML = "<div align='center'><img src='images/loading.gif' /></div>";
         }
         else {
             document.getElementById(receiveid).innerHTML = "<div align='center'><img src='images/loading.gif' /></div>";
        }
    }
 }
 function add_r_i(id, type) {
    var i_type = getType();
    if (i_type == "r_s" || i_type == "r_st") {
        if (document.getElementById('Books').checked) {
            alert("You can`t add books because you select returning of books.")
            return;
        }
        else {
            if (i_type == "r_s") {
                if (document.getElementById('Staff').checked) {
                    alert("You can`t add staff because you select return from student.First select return from student and then try again to add student.");
                    return;
                }
            }
            else {
                if (document.getElementById('Student').checked) {
                    alert("You can`t add student because you select return from staff.First select return from student and then try again to add student.");
                    return;
                }
            }
        }
     }
     else if (i_type == "i_s") {
         if (document.getElementById('Staff').checked) {
             alert("You can`t add staff because you select issue to student.First select issue to staff and then try again to add staff.");
             return;
         }
     }
     else if (i_type == "i_st") {
         if (document.getElementById('Student').checked) {
             alert("You can`t add student because you select issue to staff.First select issue to student and then try again to add student.");
             return;
         }
     }
     if (document.getElementById(id)) {
         if (type == 's') {
             var str = document.getElementById(id).innerHTML;
             document.getElementById('staffInfo').innerHTML = "<table cellpadding='2' cellspacing='0' width='100%' style='border-bottom:1px solid #000' class='r_i_font' >" + str + "</table>";
             var ele_a = document.getElementById('staffInfo').getElementsByTagName('a');
             for (var i = 0; i < ele_a.length; i++) {
                 ele_a[i].parentNode.removeChild(ele_a[i]);
             }
             document.getElementById(id).parentNode.removeChild(document.getElementById(id));
             set_ReturnDate('available');
               var addno ="";
             if (document.getElementById('sel_addno')) {
               addno=document.getElementById('sel_addno').innerHTML;
             }
             else {
               addno=document.getElementsByName('staffid')[0].value;
           }
             document.getElementById('t_addno').value = addno;
             start();
         }
         else {
             var allow = 0;
             var tot = 0;
             if (document.getElementById('allowtoissue')) {
                 allow = document.getElementById('allowtoissue').innerHTML;
             }
             var ele_access = document.getElementById('addedbooks').getElementsByTagName('input');
             for (var i = 0; i < ele_access.length; i++) {
                 if (ele_access[i].name == "accessiono") {
                     tot = parseInt(tot) + 1;
                 }
             }
             //alert("allow=" + allow + "tot" + tot);
             if ((parseInt(tot)+1) <= parseInt(allow)) {
                 if (document.getElementById("r_i_Date").value != "") {
                     var str = document.getElementById(id).innerHTML;
                     document.getElementById(id).parentNode.removeChild(document.getElementById(id));
                     add_Return_Issue_Text(str, 'available', '');
                 }
                 else {
                     alert("Please select return and issue date first.");
                     document.getElementById("r_i_Date").focus();
                 }
             }
             else {
                 alert("Issued quantity can not be greater than allow issued quantity");
             }
         }
     }
 }
 function calculate_fine() {
     var ele_fine = document.getElementsByName('TotalFine');
     var total = 0;
     for (var i = 0; i < ele_fine.length; i++) {
         if (!isNaN(ele_fine[i].value) && ele_fine[i].value != '') {
             total = parseFloat(total) + parseFloat(ele_fine[i].value);
         }
         else {
             ele_fine[i].value = ele_fine[i].getAttribute('defaultValue');
             total = parseFloat(total) + parseFloat(ele_fine[i].value);
         }
     }
     document.getElementById('total_cal_fine').innerHTML = Math.round(total*100)/100;
 }
 function add_Return_Issue_Text(str, type, date) {
     var strFine = "";
     var strIssueDate = "";
     var strReturnDate = "";
     if (document.getElementById('redit')) {
        if(document.getElementById('fedit').value=="0") {
          strFine="disabled='disabled'";
        }
        if(document.getElementById('iedit').value=="0") {
           strIssueDate="disabled='disabled'";
        }
       if (document.getElementById('redit').value == "0") {
           strReturnDate = "disabled='disabled'";
        }
     }
     var ele_addedBook = document.getElementById('addedbooks').getElementsByTagName('table');
     var len = (parseInt(ele_addedBook.length) + 1);
     var _date = document.getElementById("r_i_Date").value;
     var disable = "disabled='disabled'"
     if (type.toLowerCase() == "issued") {
         _date = date;
     }
     str += "<td colspan=\"3\">";
     str += "<table cellpadding='2' cellspacing='0' width='100%'>";
     str += "<tr><td class='r_i_font_l' style='text-align:left'>Issue Date :</td><td style='text-align:left'><span class='left'>";
     if (strIssueDate == "") {
         str += "<span class='calender' onclick=\"JACS.show(document.getElementById('IssueDate" + len + "'),event);\"></span>";
     }
     str += "<input type='text' id='IssueDate" + len + "' title='RisDate' " + strIssueDate + " name='IssueDate' value='" + _date + "'  onblur=\"chgvalidate(this);set_ReturnDate('c_date');\" style='width:90px'/></span><span class='red'  id='sIssueDate" + len + "'></span></td>";
     str += "<td class='r_i_font_l' style='text-align:right'>Return Date :</td><td style='text-align:left'><span class='left'>";
     if (strReturnDate == "") {
         str += "<span class='calender' onclick=\"JACS.show(document.getElementById('ReturnDate" + len + "'),event);\"></span>";
     }
     str += "<input type='text' id='ReturnDate" + len + "' title='RisDate' " + strReturnDate + " name='ReturnDate' onblur=\"chgvalidate(this);set_ReturnDate('c_date');\" style='width:90px'/></span><span class='red' id='sReturnDate" + len + "'></span></td>";
     if (type.toLowerCase() != "available") {
         disable = "";
     }
     disable = strFine;
     str += "<td class='r_i_font_l' style='text-align:right'>Total Fine :</td><td style='text-align:left'><input type='text' defaultValue='0' onchange='calculate_fine();' id='TotalFine" + len + "' maxlength='8' name='TotalFine' style='width:90px' value='0' "+disable+"/></td></tr>";
     str += "</table>";
     str += "</td>";
     str += "</tr>";
     var strStyle = "border-bottom:1px solid #000"
     if (type.toLowerCase() == "available") {
         strStyle = "border:1px solid green;background-color:#C2C7FF;"
     }
     str = "<table cellpadding='2' id='addedbooks" + len + "' cellspacing='0' width='100%' style='"+strStyle+"' class='r_i_font' >" + str + "</table>";
     var html = document.getElementById('addedbooks').innerHTML;
     str += html;  
     document.getElementById('addedbooks').innerHTML=str;
     for (var i = 0; i < ele_addedBook.length; i++) {
         if (ele_addedBook[i].id) {
             var ele_a = document.getElementById(ele_addedBook[i].id).getElementsByTagName('a');
             var ele_status = document.getElementById(ele_addedBook[i].id).getElementsByTagName('input');
             var status = 'available';
             for (var j = 0; j < ele_status.length; j++) {
                 if (ele_status[j].name == "status") {
                     status = ele_status[j].value;
                 }
             }
             for (var j = 0; j < ele_a.length; j++) {
                 if (status.toLowerCase() == "available") {
                     ele_a[j].innerHTML = 'Delete';
                     ele_a[j].href = 'javascript:delete_r_i(\'' + ele_addedBook[i].id + '\')';
                 }
                 else {
                     ele_a[j].innerHTML = 'Lost/Demage';
                     ele_a[j].href = 'javascript:lost_r_i(\'' + ele_addedBook[i].id + '\')';
                 }
             }
         }
     }
     set_ReturnDate(type);
 }
 
 function addDays(dat,day)
 {
    var date = new Date(dat);
    return new Date(date.getTime() + (day * 24 * 60 * 60 * 1000));
}
function dateDiff(firstDate, secondDate) {
    var fdate = new Date(firstDate);
    var sdate = new Date(secondDate);
    var frmdate = new Date(fdate.getFullYear(), fdate.getMonth(), fdate.getDate())
    var today = new Date(sdate.getFullYear(), sdate.getMonth(), sdate.getDate())
    var day = 1000 * 60 * 60 * 24;
    return ((frmdate.getTime() - today.getTime()) / (day));
    return new Date(fdate.getTime()-sdate.getTime());
}
 function getDate(date)
 {
    var rDate = date;
    if (date.indexOf("/") > 0) {
        var _tDate = date.split("/");
        rDate = _tDate[1] + "/" + _tDate[0] + "/" + _tDate[2];
    }
    return rDate;
 }
 function set_ReturnDate(type) {
     var ele = document.getElementById('staffInfo').getElementsByTagName('input');
     var date = document.getElementById('r_i_Date').value;
     for (var i = 0; i < ele.length; i++) {
         var ele_book = document.getElementById('addedbooks').getElementsByTagName('table');
         for (var j = 0; j < ele_book.length; j++) {
             var count = 0;
             var ele_inp = ele_book[j].getElementsByTagName('input');
                 var holdDay = 0;
                 var IssueDate = "";
                 var returnDate = "";
                 var areturnDate = "";
                 var issue_status = "";
                 var fineChargesPerDay = 0;
                 for (var k = 0; k < ele_inp.length; k++) {
                     if ((ele_inp[k].name == "StudentHoldDays" && ele[i].name == "addid") || (ele_inp[k].name == "StaffHoldDays" && ele[i].name == "staffid")) {
                         holdDay = ele_inp[k].value;
                         count = parseInt(count) + 1;
                     }
                     if ((ele_inp[k].name == "StudentFinePerDay" && ele[i].name == "addid") || (ele_inp[k].name == "StaffFinePerDay" && ele[i].name == "staffid")) {
                         fineChargesPerDay = ele_inp[k].value;
                         //count = parseInt(count) + 1;
                     }
                     if (ele_inp[k].name == "aReturndate") {
                         areturnDate = ele_inp[k].value;
                     }
                     if (count > 0) {
                         if (ele_inp[k].name == "status") {
                             issue_status = ele_inp[k].value;
                         }
                         if (ele_inp[k].name == "IssueDate") {
                             IssueDate = ele_inp[k].value;
                             if (IssueDate != "") {
                                 if (issue_status.toLowerCase() == "available") {
                                     if (type == "m_date") {
                                         IssueDate = date;
                                         ele_inp[k].value = date;
                                     }
                                     returnDate = new Date(addDays(getDate(IssueDate), holdDay));
                                 }
                                 else {
                                     if (type != "c_date") {
                                         returnDate = getDate(date);
                                     }
                                 }
                             }
                         }
                         if (ele_inp[k].name == "ReturnDate") {
                             var _date = "";
                             if (returnDate != "") {
                                 _date = new Date(returnDate);
                             }
                             else {
                                 _date = new Date(getDate(ele_inp[k].value));
                             }
                             ele_inp[k].value = _date.getDate() + "/" + (_date.getMonth() + 1) + "/" + _date.getFullYear();
                             returnDate = _date;
                         }
                         if (ele_inp[k].name == "TotalFine") {
                             var day = dateDiff(returnDate, getDate(areturnDate));
                             if (parseInt(day) > 0) {
                                 var _tot=parseFloat(fineChargesPerDay) * parseFloat(day);
                                 ele_inp[k].value = _tot;
                                 ele_inp[k].setAttribute('defaultValue', _tot);
                             }
			     else{   ele_inp[k].value =0;
                                 ele_inp[k].setAttribute('defaultValue', 0);}	
                         }
                     }
                 }
             }
         }
         calculate_fine();
     }
 function delete_r_i(id) {
     if (document.getElementById(id)) {
         document.getElementById(id).parentNode.removeChild(document.getElementById(id));
     }
 }

 function fillBooks(val, id,where) {
     var arr = new Array();
     var ele_access = document.getElementById('addedbooks').getElementsByTagName('input');
     var allow;
     var tot = 0;
     var status = '';
     if(document.getElementById('allowtoissue'))
     {
        allow=document.getElementById('allowtoissue').innerHTML;
     }
     var str = "";
     if (val.result.length > 0) {
         if (id == "addedbooks") {
             val.result[0]["search"] = val.result[1].search1;
         }
         if (val.result[0].search.length > 0) {
             for (var i = 0; i < val.result[0].search.length; i++) {
                 if (val.result[0].search[i].booktitle) {
                     var count = 0;
                     tot = 0;
                     for (var j = 0; j < ele_access.length; j++) {
                         if (ele_access[j].value == val.result[0].search[i].bookaccessionid) {
                             count += parseInt(count) + 1;
                             status = "Book accession no. already added."
                         }
                         if (ele_access[j].name == "accessiono") {
                             tot = parseInt(tot) + 1;
                         }
                     }
                     if (id == "addedbooks") {
                         if ((parseInt(allow)-1) < (parseInt(tot))) {
                             count += parseInt(count) + 1;
                             status = "Issued quantity can not be greater than allowed issued quantity."
                         }
                     }
                     if (parseInt(count) == 0) {
                         if (id != "addedbooks") {
                             str += "<table cellpadding='2' cellspacing='0' id='Books" + i + "' width='100%' style='border-bottom:1px solid #000' class='r_i_font' >";
                         }
                         str += "<tr>";
                         str += "<td colspan='2' class='title2'>" + val.result[0].search[i].booktitle + "</td><td style='text-align:right'><a href=\"javascript:add_r_i('Books" + i + "','b')\" style='background:url(images/addimg.png); height:16px; width:16px;'>Add</a></td>";
                         str += "</tr>";
                         str += "<tr>";
                         str += "<td class='r_i_font_l' style='width:51%'>"
                         str += "<input type='hidden' name='b_title' id='b_title" + i + "' value='" + val.result[0].search[i].booktitle + "'/>";
                         str += "<input type='hidden' name='accessiono' id='accessionno" + i + "' value='" + val.result[0].search[i].bookaccessionid + "'/>";
                         str += "<input type='hidden' name='StaffHoldDays' id='StaffHoldDays" + i + "' value='" + val.result[0].search[i].staffholddays + "'/>";
                         str += "<input type='hidden' name='StudentHoldDays' id='StudentHoldDays" + i + "' value='" + val.result[0].search[i].studentholddays + "'/>";
                         str += "<input type='hidden' name='StudentFinePerDay' id='StudentFinePerDay" + i + "' value='" + val.result[0].search[i].finechargesforstudent + "'/>";
                         str += "<input type='hidden' name='StaffFinePerDay' id='StaffFinePerDay" + i + "' value='" + val.result[0].search[i].finechargesforstaff + "'/>";
                         str += "<input type='hidden' name='b_accessionno' id='b_accessionno" + i + "' value='" + val.result[0].search[i].accessionno + "'/>";
                         str += "Accession No. : </td><td style='width:50%' colspan='2'>" + val.result[0].search[i].accessionno + "</td>";
                         str += "</tr>"
                         str += "<tr>"
                         str += "<td class='r_i_font_l'>Author : </td><td colspan='2'>" + val.result[0].search[i].author + "</td>";
                         str += "</tr>";
                         str += "<tr>"
                         str += "<td class='r_i_font_l'>Publication Name : </td><td colspan='2'>" + val.result[0].search[i].publicationname + "</td>";
                         str += "</tr>";
                         str += "<tr>"
                         str += "<td class='r_i_font_l'>Edition : </td><td colspan='2'>" + val.result[0].search[i].edition + "</td>";
                         str += "</tr>";
                         str += "<tr>"
                         str += "<td class='r_i_font_l'>Price :</td><td colspan='2'><span class='red'>" + val.result[0].search[i].price + "</span>";
                         str += "<input type='hidden' name='price' id='price" + i + "' value='" + val.result[0].search[i].price + "'/>";
                         str += "</td>";
                         str += "</tr>";
                         str += "<tr>"
                         str += "<td class='r_i_font_l'>Here you can find book : </td><td colspan='2'>" + val.result[0].search[i].location + "</td>";
                         str += "</tr>";
                         str += "<tr>"
                         str += "<td class='r_i_font_l'>status: </td><td colspan='2' class='green'><input type='hidden' id='status" + i + "' name='status' value='" + val.result[0].search[i].status + "' />" + val.result[0].search[i].status + "</td>";
                         str += "</tr>";
                         str += "<tr>"
                         str += "<td class='r_i_font_l'>Purposed Return Date : </td><td colspan='2' class='green'><input type='hidden' id='aReturndate" + i + "' name='aReturndate' value='" + val.result[0].search[i].tentativereturndate + "' />" + val.result[0].search[i].tentativereturndate + "</td>";
                         str += "</tr>";
                         if (id != "addedbooks") {
                             str += "</table>";
                         }
                         else {
                             add_Return_Issue_Text(str, val.result[0].search[i].status, val.result[0].search[i].issuedate);
                             str = "";
                         }
                     }
                 } else { status = "Book is not Available or Book Already Issued." }
             }
         } 
     }
     if (str != "") {
         if (id != "addedbooks") {
             document.getElementById(id).innerHTML = str;
         }
     }
     else {
         if (id != "addedbooks") {
             document.getElementById(id).innerHTML = "<center class='green'>No Record Found.</center>";
         }
         else {
             if (status != "") {
                 if (where!="a") {
                     alert(status);
                 }
             }
         }
     }
 }
 function fillStudents(val,id) {
     var str = "";
     var colspan = 0;
     if (val.result.length > 0) {
         if (val.result[0].search) {
             if (val.result[0].search.length > 0) {
                 for (var i = 0; i < val.result[0].search.length; i++) {
                     if (val.result[0].search[i].studentname) {
                         str += "<table cellpadding='2' cellspacing='0' id='student" + i + "' width='100%' style='border-bottom:1px solid #000' class='r_i_font' >";
                         str += "<tr>"
                         str += "<td colspan='2' class='title2'>" + val.result[0].search[i].studentname + "</td><td style='text-align:right'>"
                         if (id == "searchResult") {
                             str += "<a href=\"javascript:add_r_i('student" + i + "','s')\" style='background:url(images/addimg.png); height:16px; width:16px;'>Add</a>";
                         }
                         str += "</td>";
                         str += "</tr>"
                         str += "<tr>"
                         str += "<td class='r_i_font_l' style='width:51%'>"
                         str += "<input type='hidden' name='addid' id='addid" + i + "' value='" + val.result[0].search[i].addid + "'/>";
                         str += "<input type='hidden' name='stu_name' id='stu_name" + i + "' value='" + val.result[0].search[i].studentname + "'/>";
                         str += "<input type='hidden' name='stu_addno' id='stu_addno" + i + "' value='" + val.result[0].search[i].addno + "'/>";
                         str += "add No. : </td><td style='width:50%' id='sel_addno' colspan='2'>" + val.result[0].search[i].addno + "</td>";
                         if (id != "searchResult") {
                             var img = ""
                             if (val.result[0].search[i].imageurl != "") {
                                 img = "<img src='studata/" + val.result[0].search[i].imageurl + "' height='100px' width='100px' />"
                             }
                             else {
                                 img = "<img src='Images/SUPERVISOR.png' height='100px' width='100px' />"
                             }
                             str += "<td rowspan='7'><div style='height:100px; width:100px; border:1px solid #000;margin-right:28px'>" + img + "</div></td>"
                         } else {
                         colspan = 2;
                         }
                         str += "</tr>"
                         str += "<tr>"
                         str += "<td class='r_i_font_l'>Roll No. : </td><td colspan='" + colspan + "'>" + val.result[0].search[i].rollno + "</td>";
                         str += "</tr>";
                         str += "<tr>"
                         str += "<td class='r_i_font_l'>Course : </td><td colspan='" + colspan + "'>" + val.result[0].search[i].coursetitle + "</td>";
                         str += "</tr>";
                         str += "<tr>"
                         str += "<td class='r_i_font_l'>Semester : </td><td colspan='" + colspan + "'>" + val.result[0].search[i].semname + "</td>";
                         str += "</tr>";
                         str += "<tr>"
                         str += "<td class='r_i_font_l'>DOB : </td><td colspan='" + colspan + "'>" + val.result[0].search[i].dob + "</td>";
                         str += "</tr>";
                         str += "<tr>"
                         str += "<td class='r_i_font_l'>Issued Qty : </td><td colspan='" + colspan + "' class='red'>" + val.result[0].search[i].issued + "</td>";
                         str += "</tr>";
                         str += "<tr>"
                         str += "<td class='r_i_font_l'>Allow to Issue : </td><td colspan='" + colspan + "'><span class='red' id='allowtoissue'>" + val.result[0].search[i].totalbook + "</span></td>";
                         str += "</tr>";
                         str += "</table>";
                     }
                     else {
                         document.getElementById(id).innerHTML = "<center class='green'>No Record Found.</center>";
                     }
                 }
             } else {
                 document.getElementById(id).innerHTML = "<center class='green'>No Record Found.</center>";
             }
         }
     }
     if (str != "") {
         document.getElementById(id).innerHTML = str;
     }
     else {
         document.getElementById(id).innerHTML = "<center class='green'>No Record Found.</center>";
     }
 }
 function fillStaff(val,id) {
     var str = "";
     var colspan = 0;
     if (val.result.length > 0) {
         if (val.result[0].search)
         {
             if (val.result[0].search.length > 0) {
                 for (var i = 0; i < val.result[0].search.length; i++) {
                     if (val.result[0].search[i].acname) {
                         str += "<table cellpadding='2' cellspacing='0' id='staff" + i + "' width='100%' style='border-bottom:1px solid #000' class='r_i_font' >";
                         str += "<tr>"
                         str += "<td colspan='2' class='title2'>" + val.result[0].search[i].acname + "</td><td style='text-align:right'>"
                         if (id == "searchResult") {
                             str += "<a href=\"javascript:add_r_i(\'staff" + i + "','s')\" style='background:url(images/addimg.png); height:16px; width:16px;'>Add</a>";
                         }
                         str += "</td>";
                         str += "</tr>"
                         str += "<tr>"
                         str += "<td class='r_i_font_l'>Staff Card Id. : </td><td colspan='2'>" + val.result[0].search[i].staffcardid + "</td>";
                         if (id != "searchResult") {
                             var img=""
                             if (val.result[0].search[i].staffimage != "") {
                                 img = "<img src='StaffImage/" + val.result[0].search[i].staffimage + "' height='100px' width='100px' />"
                             }
                             else {
                                 img = "<img src='Images/SUPERVISOR.png' height='100px' width='100px' />"
                             }
                             str += "<td rowspan='7'><div style='height:100px; width:100px; border:1px solid #000;margin-right:28px'>" + img + "</div></td>"
                         } else {
                             colspan = 2;
                         }
                         str += "</tr>";
                         str += "<tr>"
                         str += "<td class='r_i_font_l' style='width:51%'>";
                         str += "<input type='hidden' name='staffid' id='staffid" + i + "' value='" + val.result[0].search[i].staffid + "'/>";
                         str += "<input type='hidden' name='staffname' id='staffname" + i + "' value='" + val.result[0].search[i].acname + "'/>";
                         str += "<input type='hidden' name='department' id='department" + i + "' value='" + val.result[0].search[i].groupname + "'/>";
                         str += "Mobile No. : </td><td style='width:50%' colspan='"+colspan+"'>" + val.result[0].search[i].mobileno + "</td>";
                         str += "</tr>"
                         str += "<tr>"
                         str += "<td class='r_i_font_l'>Father Name. : </td><td colspan='" + colspan + "'>" + val.result[0].search[i].fathername + "</td>";
                         str += "</tr>";
                         str += "<tr>"
                         str += "<td class='r_i_font_l'>Email Id : </td><td colspan='" + colspan + "'>" + val.result[0].search[i].emailid + "</td>";
                         str += "</tr>";
                         str += "<tr>"
                         str += "<td class='r_i_font_l'>Department : </td><td colspan='" + colspan + "'>" + val.result[0].search[i].groupname + "</td>";
                         str += "</tr>";
                         str += "<tr>";
                         str += "<td class='r_i_font_l'>Issued Qty : </td><td colspan='" + colspan + "' class='red'>" + val.result[0].search[i].issued + "</td>";
                         str += "</tr>";
                         str += "<tr>"
                         str += "<td class='r_i_font_l'>Allow to Issue : </td><td colspan='" + colspan + "'><span class='red' id='allowtoissue'>" + val.result[0].search[i].totalbook + "</span></td>";
                         str += "</tr>";
                         str += "</table>";
                     }
                     else {
                         document.getElementById(id).innerHTML = "<center class='green'>No Record Found.</center>";
                     }
                 }
             } else {
                 document.getElementById(id).innerHTML = "<center class='green'>No Record Found.</center>";
             } 
         }
     }
     if (str != "") {
         document.getElementById(id).innerHTML = str;
     }
     else {
         document.getElementById(id).innerHTML = "<center class='green'>No Record Found.</center>";
     }
 }
 function fillInfo(val, id) {
   var i_r_type = getType();
   if (id == "addInfo") {
       if (i_r_type == "i_st" || i_r_type == "r_st") {
             fillStaff(val, 'staffInfo');
         }
         else {
             fillStudents(val, 'staffInfo');
         }
         var ele=document.getElementById("staffInfo").getElementsByTagName('table');
         if (parseInt(ele.length) > 0) {
             fillBooks(val, 'addedbooks','a');
         }
     }
     else {
         fillBooks(val, 'addedbooks','b');
     }
         document.getElementById("serror").innerHTML = "";
     }
 function get_info() {
     var accessionno = document.getElementById('t_accessionno').value;
     var addno = document.getElementById('t_addno').value;
     var type = "s"
     var i_r_type = getType();
     if (i_r_type == "i_st" || i_r_type == "r_st") {
         type = "st"
     }
     if (addno != "" || accessionno != "") {
         if (i_r_type == "i_s" || i_r_type == "i_st" || addno != '') {
             var id = "addInfo";
             if (accessionno != "") {
                 id = "bookInfo";
             }
             senddata_post('Retrun_and_Issue', 'T=info&accessiono=' + accessionno + '&addno=' + addno + '&type=' + type, id);
         }
         else {
             var ele_accessionno = document.getElementById('addedbooks').getElementsByTagName('td');
             for (var i = 0; i < ele_accessionno.length; i++) {
                 if ((ele_accessionno[i].innerHTML).toUpperCase() == (accessionno).toUpperCase()) {
                     var id = ele_accessionno[i].parentNode.parentNode.parentNode.id;
                     document.getElementById(id).setAttribute("style", "border:1px solid green;background-color:#C2C7FF;");
                     document.getElementById(id).setAttribute("return", "Y");
                     document.getElementById(id).getElementsByTagName('a')[0].innerHTML = "Cancel Return";
                     document.getElementById(id).getElementsByTagName('a')[0].href = "javascript:cancel_r(\""+id+"\")";
                 }
             }
         }
         document.getElementById('t_addno').value = '';
         document.getElementById('t_accessionno').value = '';
         document.getElementById('t_accessionno').focus();
     }
 }
 function cancel_r(id) {
     document.getElementById(id).removeAttribute('return');
     document.getElementById(id).setAttribute("style", "border-bottom:1px solid #000");
     document.getElementById(id).getElementsByTagName('a')[0].innerHTML = "Lost/Damage";
     document.getElementById(id).getElementsByTagName('a')[0].href = "javascript:lost_r_i(\'"+id+"\')";
 }
 function save_r_i(val) {
     var ele_staff_student = document.getElementById('staffInfo').getElementsByTagName('input');
     var ele_tab = document.getElementById('addedbooks').getElementsByTagName('table');
     var type = ''
     var personid = "";
     var accessionid = "";
     var issuedate = "";
     var returndate = "";
     var areturndate = "";
     var totalfine = 0;
     for (var i = 0; i < ele_staff_student.length; i++) {
         if (ele_staff_student[i].name == 'addid' || ele_staff_student[i].name == 'staffid') {
             personid = ele_staff_student[i].value;
         }
     }
     var i_r_type = getType();
     for (var k = 0; k < ele_tab.length; k++) {
         var count = 0;
         var ele_Books = ele_tab[k].getElementsByTagName('input');
         if (i_r_type=="i_st" || i_r_type=="i_s") {
             count = 1;
             type = "Issue";
         }
         else {
             if (ele_tab[k].getAttribute('return') == "Y") {
                 count = parseInt(count) + 1;
                 if (val == "r") {
                     type = "Reissue"; 
                 }
                 else {
                     type = "Return";
                 }
             }
         }
         if (count > 0) {
             for (var i = 0; i < ele_Books.length; i++) {
                 if (ele_Books[i].name == "accessiono") {
                     accessionid += ele_Books[i].value + ",";
                 }
                 else if (ele_Books[i].name == "IssueDate") {
                     issuedate += ele_Books[i].value + ",";
                 }
                 else if (ele_Books[i].name == "ReturnDate") {
                     returndate += ele_Books[i].value + ",";
                 }
                 else if (ele_Books[i].name == "TotalFine") {
                     totalfine += ele_Books[i].value + ",";
                 }
                 else if (ele_Books[i].name == "aReturndate") {
                     areturndate += ele_Books[i].value + ",";
                 }
             }
         }
     }
         var date = "";
         date = document.getElementById('r_i_Date').value;
         if (validate()) {
             var issueto = "Staff";
             if (i_r_type == "r_s" || i_r_type == "i_s") {
                 issueto = "Student";
             }
             if (personid != "" && accessionid != "" && date != "") {
                 senddata_post("Retrun_and_Issue", "T=save_retunandissue&type=" + type + "&personid=" + personid + "&date=" + document.getElementById('r_i_Date').value + "&accessionid=" + accessionid + "&issuedate=" + issuedate + "&returndate=" + returndate + "&totalfine=" + totalfine + "&issueto=" + issueto + "&areturndate=" + areturndate, "serror");
                 //document.getElementById("t_addno").focus();
             }
             else {
                 alert("Please select at least one book for issue or return.");
             }
         } else {
             alert("Please select at least one book for issue or return.");
         }
     }

     /*Lost And Demage*/
     function lost_r_i(id) {
         var accessionid = "";
         var personid = "";
         var price = 0;
         var accessiono = "";
         var totalfine = 0;
         var StudentName = "";
         var StaffName = "";
         var addno = "";
         var department = "";
         var title = "";
     var ele_staff_student = document.getElementById('staffInfo').getElementsByTagName('input');
     var ele_Books = document.getElementById(id).getElementsByTagName("input");
     var type = getType();
       for (var i = 0; i < ele_staff_student.length; i++) {
           if (ele_staff_student[i].name == 'addid' || ele_staff_student[i].name == 'staffid') {
                   personid = ele_staff_student[i].value;
           } else if (ele_staff_student[i].name == 'stu_addno' || ele_staff_student[i].name == 'department') {
                   addno = ele_staff_student[i].value;
           } else if (ele_staff_student[i].name == 'stu_name' || ele_staff_student[i].name == 'staffname') {
                   StudentName = ele_staff_student[i].value;
           }
     }
     for (var i = 0; i < ele_Books.length; i++) {
         if (ele_Books[i].name == "accessiono") {
             accessionid = ele_Books[i].value;
         }
         else if (ele_Books[i].name == "b_accessionno") {
             accessiono = ele_Books[i].value;
         }
         else if (ele_Books[i].name == "b_title") {
             title = ele_Books[i].value;
         }
         else if (ele_Books[i].name == "price") {
             price = ele_Books[i].value;
         }
         else if (ele_Books[i].name == "TotalFine") {
             totalfine = ele_Books[i].value;
         }
     }
     var strHTML = "<table cellpadding='5' cellspacing='0' width='100%'>"
        strHTML += "<tr>";
        strHTML += "<td class=\"title\" height=\"33\" style='font-size:16px'>Book Lost/Damage Fine";
        strHTML += "<input type='hidden' id='f_lostid' name='f_lostid' value='" + id + "'/>";
        strHTML += "<input type='hidden' id='f_personid' name='f_personid' value='" + personid + "'/>";
        strHTML += "<input type='hidden' id='f_accessionid' name='f_accessionid' value='"+accessionid+"'/>";
        strHTML += "</td>";
        strHTML += "<td class=\"title\" align=\"right\"><a href='javascript:void(0);' onclick='cancel_lost()' class='close' title='close'></a></td>";
        strHTML += "</tr>";
        strHTML += "<tr>";
        strHTML += "<td colspan='2' style='text-align:center'><span id='lost_error'></span></td>";
        strHTML += "</tr>";
     if (type == "i_s" || type == "r_s") {
         strHTML += "<tr>";
         strHTML += "<td style='text-align:right'>";
         strHTML+="<input type='hidden' id='f_type' name='f_type' value='Student'/>";
         strHTML+="Student Name :</td><td><b>" + StudentName + "</b></td>";
         strHTML += "</tr>";
         strHTML += "<tr>";
         strHTML += "<td style='text-align:right'>Add No. :</td><td><b>" + addno + "</b></td>";
         strHTML += "</tr>";
     }
     else {
         strHTML += "<tr>";
         strHTML += "<td style='text-align:right'>";
         strHTML += "<input type='hidden' id='f_type' name='f_type' value='Staff'/>";
         strHTML+="Staff Name :</td><td><b>" + StudentName + "</b></td>";
         strHTML += "</tr>";
         strHTML += "<tr>";
         strHTML += "<td style='text-align:right'>Department :</td><td><b>" + addno + "</b></td>";
         strHTML += "</tr>";
     }
     strHTML += "<tr>";
     strHTML += "<td style='text-align:right'>Book Title :</td><td><b>"+title+"</b></td>";
     strHTML += "</tr>";
     strHTML += "<tr>";
     strHTML += "<td style='text-align:right'>Accession No :</td><td><b>" + accessiono + "</b></td>";
     strHTML += "</tr>";
     strHTML += "<tr>";
     strHTML += "<td style='text-align:right'>Book Price :</td><td><input name='f_price' onchange='cal_tot()' id='f_price' value='" + price + "'/></td>";
     strHTML += "</tr>";
     strHTML += "<tr>";
     strHTML += "<td style='text-align:right'>Total Fine :</td><td><input name='f_fine' id='f_fine' onchange='cal_tot()' value='" + totalfine + "'/></td>";
     strHTML += "</tr>";
     strHTML += "<tr>";
     var total = 0;
     total = parseFloat(price) + parseFloat(totalfine);
     if (isNaN(total)) {
        total=0;
     }
     strHTML += "<td style='text-align:right'>Total :</td><td><span id='f_tot'><b>"+total+"</b></span></td>";
     strHTML += "</tr>";
     strHTML += "<tr>";
     strHTML += "<td style='text-align:right'>Description :</td><td><textarea name='f_description' id='f_description'></textarea></td>";
     strHTML += "</tr>";
     strHTML += "<tr>";
     strHTML += "<td style='text-align:right'><input type='button' id='fineOK' name='fineOK' onclick='save_lost();' value='Lost/Damage' /></td>";
     strHTML += "<td style='text-align:left'><input type='button' id='finecancel' name='finecancel' value='Cancel' onclick='cancel_lost();' /></td>";
     strHTML += "</tr>";
     strHTML += "</table>";
     document.getElementById('popup').innerHTML = strHTML;
     document.getElementById('popup').className = "popup";
     document.getElementById('blank').className = "blank1";
 }
 function cancel_lost() {
     document.getElementById('popup').innerHTML = "";
     document.getElementById('popup').className = "none";
     document.getElementById('blank').className = "none";
 }
 function cal_tot() {
     var total = 0;
     if (document.getElementById('f_price')) {
         if (!isNaN(document.getElementById('f_price').value)) {
             total = parseFloat(total) + parseFloat(document.getElementById('f_price').value);
         } else {
         document.getElementById('f_price').value = 0;
         }
     }
     if (document.getElementById('f_fine')) {
         if (!isNaN(document.getElementById('f_fine').value)) {
             total = parseFloat(total) + parseFloat(document.getElementById('f_fine').value);
         } else {
         document.getElementById('f_fine').value = 0;
         }
     }
     document.getElementById('f_tot').innerHTML = total;
 }
 function save_lost() {
     var accessionid = "";
     var personnid = "";
     var type = "";
     var price = "";
     var fine = "";
     var description = "";
     accessionid = returnValue("f_accessionid");
     personnid = returnValue("f_personid");
     type = returnValue("f_type");
     price = returnValue("f_price");
     fine = returnValue("f_fine");
     description = returnValue("f_description");
     senddata_post("Retrun_and_Issue", "T=save_booklost&type=" + type + "&personid=" + personnid + "&date=" + document.getElementById('r_i_Date').value + "&accessionid=" + accessionid + "&totalfine=" + fine + "&additionalfine=" + price + "&description=" + description, "lost_error");
 }
 function returnValue(name)
 {
    var value="";
    if(document.getElementById(name))
    {
      value=document.getElementById(name).value
    }
    return value;
}
function print() {
    var win = window.open();
    var str = "<table cellpadding='0' cellspacing='0' width='100%'>";
    str += "<tr><td style='text-align:center'><span><b>Mata Gujri College Fatehgarh Sahib</b></span></td></tr>";
    str += "<tr><td>" + document.getElementById('print_books').innerHTML + "</td></tr>"
    str += "</table>"
    win.document.write(str);
    win.print();
}